package com.xkdx.guangguang.fragment.shop.uploadphoto;

import java.util.HashMap;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.UpLoadPhotoChoiceActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class UploadBranchPhotoFragment extends BaseFragment {
	View view;
	Button back, upLoad;
	Spinner sp;
	EditText photoTitle;
	ImageView updatePicture;
	ProgressDialog pd;
	Bitmap upLoadBitmap;
	String shopID;
	String brandID;
	String tag, userID, loginToken;
	ScrollView softkey_scrollview;
	String GalleryID = "1";
	int choiced = -2;
	public static boolean isUploadSuccess = false;
	String[] photo_classifications = { "店铺橱窗及外观", "店铺内部", "产品详情及促销", "真人秀及其它" };
	UploadPhotoAction uploadPhotoAction;
	UploadPhotoModule uploadPhotoModule;
	UploadPhotoPresistence uploadPhotoPresistence;
	public int position;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SharePrefenceUtil su = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
		userID = su.getUserID();
		loginToken = su.getUserLoginToken();
		shopID = getArguments().getString("shopID");

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.shop_photo_upload, container, false);
		findView();
		setOnClick();

		return view;
	}

	@Override
	public void findView() {
		back = (Button) view.findViewById(R.id.back_shop_photo_upload);
		photoTitle = (EditText) view.findViewById(R.id.photo_title);
		upLoad = (Button) view.findViewById(R.id.photo_update);
		updatePicture = (ImageView) view.findViewById(R.id.upLoad_choice);
		softkey_scrollview = (ScrollView) view.findViewById(R.id.softkey_scrollview);
		sp = (Spinner) view.findViewById(R.id.choice_photo_classificaton);
		updatePicture.setImageBitmap(UpLoadPhotoChoiceActivity.shopUpLoadPicture);

	}

	@Override
	public void setOnClick() {

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFragmentManager().popBackStack();
			}
		});
		upLoad.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showLoading();

			}
		});

		photoTitle.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				final int y = photoTitle.getLineHeight();
				if (hasFocus) { // 获取到焦点
					softkey_scrollview.post(new Runnable() {
						@Override
						public void run() {
							softkey_scrollview.smoothScrollTo(0, photoTitle.getSelectionStart() + 2 * y);
						}
					});
				}
			}
		});

		photoTitle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final int y = photoTitle.getLineHeight();
				softkey_scrollview.post(new Runnable() {
					@Override
					public void run() {
						softkey_scrollview.smoothScrollTo(0, photoTitle.getSelectionStart() + 2 * y);
					}
				});
			}
		});

		photoTitle.addTextChangedListener(new TextWatcher() {
			private CharSequence temp;
			private boolean isEdit = true;
			private int selectionStart;
			private int selectionEnd;

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
				temp = s;
			}

			@Override
			public void afterTextChanged(Editable s) {
				selectionStart = photoTitle.getSelectionStart();
				selectionEnd = photoTitle.getSelectionEnd();
				if (temp.length() > 20) {
					Toast.makeText(getActivity(), getResources().getString(R.string.beyond_limit), Toast.LENGTH_SHORT)
							.show();
					s.delete(selectionStart - 1, selectionEnd);
					int temSelection = selectionStart;
					photoTitle.setText(s);
					photoTitle.setSelection(temSelection);
				}

			}
		});
		PhotoChoiceAdapter adapter = new PhotoChoiceAdapter(getActivity(), photo_classifications);

		sp.setAdapter(adapter);
		sp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// Log.i(TAG, position + 1 + "");
				GalleryID = "" + (position + 1);
				choiced = position;
				TextView tv = (TextView) view.findViewById(R.id.tv_photo_choice);
				tv.setTextColor(getResources().getColor(R.color.photo_item_choice));
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

	}

	@Override
	public void showLoading() {

		uploadPhotoAction = new UploadPhotoAction(userID, loginToken, shopID, GalleryID, photoTitle.getText()
				.toString());
		uploadPhotoModule = new UploadPhotoModule();
		uploadPhotoPresistence = new UploadPhotoPresistence(this);
		((ManagerActivity) getActivity()).addCurrentTask(uploadPhotoPresistence);
		uploadPhotoPresistence.setActitons(uploadPhotoAction);
		uploadPhotoPresistence.setModule(uploadPhotoModule);
		uploadPhotoPresistence.execute();

		dataLoadDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				if (uploadPhotoPresistence != null) {
					uploadPhotoPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();
			}
		});
		dataLoadDialog.show();

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			if (uploadPhotoModule.isUploadSuccess) {
				isUploadSuccess = true;
				getFragmentManager().popBackStack();
				Toast.makeText(getActivity(), "上传成功", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getActivity(), uploadPhotoModule.message, Toast.LENGTH_SHORT).show();
			}
		} else {
			showErrorTip(map);
		}

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDestroy() {
		if (UpLoadPhotoChoiceActivity.shopUpLoadPicture != null) {
			UpLoadPhotoChoiceActivity.shopUpLoadPicture.recycle();
			UpLoadPhotoChoiceActivity.shopUpLoadPicture = null;
		}
		super.onDestroy();
	}

	class PhotoChoiceAdapter extends BaseAdapter {
		LayoutInflater inflater;
		String[] str;

		public PhotoChoiceAdapter(Context context, String[] str) {
			inflater = LayoutInflater.from(context);
			this.str = str;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return str.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return str[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.photo_choice_tv, null);
			}
			TextView tv = (TextView) convertView.findViewById(R.id.tv_photo_choice);
			ImageView im = (ImageView) convertView.findViewById(R.id.im_choice);
			if (position == choiced) {
				tv.setTextColor(getResources().getColor(R.color.photo_item_choice));
				im.setVisibility(View.VISIBLE);
			} else {
				tv.setTextColor(getResources().getColor(R.color.photo_item_nochoice));
				im.setVisibility(View.GONE);
			}
			tv.setText(str[position]);

			return convertView;
		}

	}

}
